//package com.service.impl;
//import com.pojo.Department;
//import com.pojo.Employee;
//import com.mapper.DepartmentMapper;
//import com.mapper.EmployeeMapper;
//import com.service.DepartmentService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Service
//public class DepartmentServiceImpl implements DepartmentService {
//    @Autowired
//    private DepartmentMapper departmentMapper;
//    @Autowired
//    private EmployeeMapper employeeMapper;
//
//    // ---------------------- 基础CRUD操作 ----------------------
//    @Override
//    public void add(Department department) {
//        departmentMapper.insert(department);
//    }
//
//    @Override
//    public void update(Department department) {
//        departmentMapper.updateById(department);
//    }
//
//    // ---------------------- 复杂业务逻辑 ----------------------
//    @Override
//    public List<Department> getDepartmentTree() {
//        List<Department> allDepartments = departmentMapper.selectAll();
//        return buildTree(allDepartments, 0); // 从根节点（parentId=0）开始构建
//    }
//
//    @Transactional
//    @Override
//    public void setManager(Integer departmentId, Integer employeeId) {
//        // 验证部门存在性
//        Department department = departmentMapper.selectById(departmentId);
//        if (department == null) {
//            throw new RuntimeException("部门不存在");
//        }
//
//        // 验证员工存在性
//        Employee employee = employeeMapper.selectById(employeeId);
//        if (employee == null) {
//            throw new RuntimeException("员工不存在");
//        }
//
//        // 更新负责人
//        department.setManagerId(employeeId);
//        departmentMapper.updateById(department);
//    }
//
//    // ---------------------- 私有工具方法 ----------------------
//    private List<Department> buildTree(List<Department> list, Integer parentId) {
//        return list.stream()
//                .filter(d -> parentId.equals(d.getParentId()))
//                .peek(d -> d.setChildren(buildTree(list, d.getId())))
//                .collect(Collectors.toList());
//    }
//}
